#ifndef __SLOTHTTPD_STAGES_RECEIVE_REQUEST_HPP__
#define __SLOTHTTPD_STAGES_RECEIVE_REQUEST_HPP__

#include <slothttpd/ref_counted.hpp>
#include <slothttpd/parser/request_line_parser.hpp>
#include <slothttpd/parser/headers_parser.hpp>

namespace slothttpd {
namespace stage {

class receive_request;
typedef boost::intrusive_ptr<receive_request> receive_request_ptr;

class receive_request :
        public ref_counted
{
public:
    receive_request(connection_ptr conn);
    
private:
    void post_read();
    static void handle_read(receive_request_ptr s, const boost::system::error_code &err, std::size_t length);
    parser::result_type do_parsing();
    static void handle_read_content(receive_request_ptr s, const boost::system::error_code &err);
    void header_found(char_range_t name, char_range_t value);
    
    enum state {
        parse_request_line,
        parse_headers,
        parse_done
    };
    
    parser::request_line_parser request_line_parser_;
    parser::headers_parser      headers_parser_;
    
    state                       state_;
};

}
}

#endif
